package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DefectreformreportHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    /**
     * 缺陷整改单设置节点自定义审批人
     * @param app
     * @param id
     * @param flowNode
     * @param vars
     * @return
     * @throws Exception
     */
    public String addFlowCandidate(String app, Long id, FlowNode flowNode, Map<String, Object> vars) throws Exception {
        String executer = null;
        Map<String, String> map = new HashMap<>();
        map.put("shzrr","charge");
        map.put("zgzx","charge");
        map.put("zgqr","confirmer");
        map.put("wcys","accetpperson");
        if(StringUtils.isNotBlank(map.get(flowNode.getId()))){
            //拿到deptcode
            List<Map<String, Object>> defectreport = handlerService.findBySql("select charge,accetpperson,confirmer from bl_defectreport where id=" + id);
            executer = MapUtil.getStr(defectreport.get(0),map.get(flowNode.getId()));
        }
        return executer;
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if("tbrtj".equals(flowNode.getId())){//获取到提报人提交节点
            boolean flag = op.isFlag();//是否提交
            if (flag){
                //缺陷整改单提交以后，将缺陷提报单的整改选择字段变为1
                Map<String, Object> map = handlerService.findMainById(app,id);//获取缺陷整改单主对象
                String reportnum= MapUtils.getString(map,"reportnum");//获取缺陷提报单的编号
                if (!"".equals(reportnum)&&reportnum!=null){
                    handlerService.updateBySql("UPDATE bl_defectreport SET zgflag=1  WHERE  reportnum in ( SELECT reportnum from bl_defectline where reportchangenum='"+reportnum+"')" );
                }
            }
        }
        super.afterExcuteFlow(app, id, flowNode, op, event);
    }
}
